#!/usr/bin/env python

__author__ = 'rx.wen218@gmail.com'

import httplib
import atom.core
import gdata.blogger.data
from post import Post
from datetime import datetime

class DoubanProvider:
    def __init__(self, user_name):
        self.user_name = user_name

    def GetPosts(self, max_posts_num = 9999):
        host = "api.douban.com"
        con = httplib.HTTPConnection(host)
        con.request("GET", "/people/%s/notes"%self.user_name)
        response = con.getresponse()
        str = response.read()
        douban_posts = atom.core.Parse(str, gdata.blogger.data.BlogPostFeed)
        posts = [Post(
            postid = post.id.text,
            title = post.title.text, 
            summary = post.content.text, 
            content = post.content.text, 
            create_date = datetime.strptime(post.published.text[:-6], '%Y-%m-%dT%H:%M:%S'), 
            tags = [category.term for category in post.category]) for post in douban_posts.entry]
        return posts
        
    def AddPost(self, post):
        raise NotImplementedError("AddPost hasn't been implemented yet")

    def UpdatePost(self, post):
        raise NotImplementedError("UpdatePost hasn't been implemented yet")

    def DeleteAllPosts(self):
        raise NotImplementedError("DeleteAllPosts hasn't been implemented yet")
